// JavaScript Document
var a_map;

function start_gps(counter, placeholdername)
{
    <!--map.style.display ='none';-->
    <!--document.getElementById('cur_position').innerHTML = "Getting geolocation . . .";-->

	var onSuccess = function(pos)
	{
		latvalue = pos.coords.latitude;
		longvalue = pos.coords.longitude;
		console.log("Latitude=" + latvalue + "; longitude=" + longvalue);
		
		var myLatlng = new google.maps.LatLng(latvalue, longvalue);
		
		/*Displaying location on a map*/
		if (counter == 1)
		{
				 getAddressFromLatLang(latvalue,longvalue, placeholdername);
		}
		else if (counter == 2)
		{
			var opts = 
			{
			 center: myLatlng,
			 zoom: 15
			};
			a_map = new google.maps.Map(document.getElementById('gps_map'), opts);
			google.maps.event.trigger(a_map,'resize');					

			 var marker = new google.maps.Marker(
			 {
			  position: myLatlng,
			  map: a_map,
			  title: 'Map'
			 })
		}
		else if (counter == 3)
		{
			$('#startj').removeClass('ui-btn-up-c ui-btn-hover-c ui-btn-down-c').addClass('ui-btn-up-a ui-btn-hover-a ui-btn-down-a');
			if (monitorgps() == 'undefined' || monitorgps() == null)
			{
				console.log("Location didn't change!");	
				alert("No change in location detected!");
			}
			else
			{
				var destLatlng = monitorgps();
				
				var request =
				{
					origin: myLatlng,
					destination: destLatlng,
					travelMode: google.maps.TravelMode.DRIVING,
					provideRouteAlternatives: true,
					durationInTraffic: true
				};
				directionsDisplay = new google.maps.DirectionsRenderer();
				directionsDisplay.setMap(a_map);
				var a_directionsService = new google.maps.DirectionsService();
				
				a_directionsService.route(request, function(response, status) 
				{
				 if (status == google.maps.DirectionsStatus.OK) 
				 {
					directionsDisplay.setDirections(response);
				 }
				});
			}
		}
	};
	
	var onFailure = function(error)
	{
		alert('code: '    + error.code    + '\n' +
			   'message: ' + error.message + '\n');
		console.log("Error getting geolocation: code=" + error.code + " message=" + error.message);
	};
	
    console.log("Getting geolocation . . .");
    navigator.geolocation.getCurrentPosition(onSuccess, onFailure, {
  enableHighAccuracy: true,
  timeout: 30000,
  maximumAge: 30000
});
	
};

function monitorgps()
{
	console.log("monitorgps called...");
	var watchID = navigator.geolocation.watchPosition(onsuccess, onfailure);
	
	var onsuccess = function(pos) 
	{
		if (watchID!=null)
		{
			$("a[name='endj']").removeClass('ui-disabled');
		}
		var destLatlng = new google.maps.LatLng(pos.coords.latitude, pos.coords.longitude);
		console.log("Dest lat lng are: " + destLatlng);
  	return destLatlng;
    };
	
	var onfailure = function(error)
	{
		alert('code: '    + error.code    + '\n' +
			   'message: ' + error.message + '\n');
		console.log("Error getting geolocation: code=" + error.code + " message=" + error.message);
		return null;
	}
}

function getAddressFromLatLang(lat,lng, placeholdername){
      var geocoder = new google.maps.Geocoder();
      var latLng = new google.maps.LatLng(lat, lng);
	  
      geocoder.geocode( {'latLng': latLng}, function(results, status) {
      console.log("After getting address");
      //console.log(results);

        if (status == google.maps.GeocoderStatus.OK) 
		{
          if (results[1]) {
			  //alert(results[1].formatted_address);
			  if (placeholdername == 1)
			  {
				document.getElementById('from').value = results[1].formatted_address;  
			    $('#usegpsin-from').removeClass('ui-btn-up-c ui-btn-hover-c ui-btn-down-c').addClass('ui-btn-up-b ui-btn-hover-b ui-btn-down-b').attr('data-theme', 'b');
			    $('#usehomein-from').removeClass('ui-btn-up-b ui-btn-hover-b ui-btn-down-b').addClass('ui-btn-up-c ui-btn-hover-c ui-btn-down-c').attr('data-theme', 'c');
			    $('#useworkin-from').removeClass('ui-btn-up-b ui-btn-hover-b ui-btn-down-b').addClass('ui-btn-up-c ui-btn-hover-c ui-btn-down-c').attr('data-theme', 'c');	  	  
				$('#tourist-from').removeClass('ui-btn-up-f ui-btn-hover-f ui-btn-down-f').addClass('ui-btn-up-c ui-btn-hover-c ui-btn-down-c').attr('data-theme', 'c');
				
			  }
			  else if (placeholdername == 2)
			  {
				document.getElementById('to').value = results[1].formatted_address;  
			    $('#usegpsin-to').removeClass('ui-btn-up-c ui-btn-hover-c ui-btn-down-c').addClass('ui-btn-up-b ui-btn-hover-b ui-btn-down-b').attr('data-theme', 'b');
			    $('#usehomein-to').removeClass('ui-btn-up-b ui-btn-hover-b ui-btn-down-b').addClass('ui-btn-up-c ui-btn-hover-c ui-btn-down-c').attr('data-theme', 'c');
			    $('#useworkin-to').removeClass('ui-btn-up-b ui-btn-hover-b ui-btn-down-b').addClass('ui-btn-up-c ui-btn-hover-c ui-btn-down-c').attr('data-theme', 'c');
				$('#tourist-to').removeClass('ui-btn-up-f ui-btn-hover-f ui-btn-down-f').addClass('ui-btn-up-c ui-btn-hover-c ui-btn-down-c').attr('data-theme', 'c');				
				
			  }
			  else if (placeholdername == 3)
			  {
				document.getElementById('homelocationsaver').value = results[1].formatted_address;  
				$('#save').removeClass('ui-btn-up-e ui-btn-hover-e ui-btn-down-e').addClass('ui-btn-up-c ui-btn-hover-c ui-btn-down-c');  
			  }
			  else if (placeholdername == 4)
			  {
				document.getElementById('worklocationsaver').value = results[1].formatted_address;  
				$('#save2').removeClass('ui-btn-up-e ui-btn-hover-e ui-btn-down-e').addClass('ui-btn-up-c ui-btn-hover-c ui-btn-down-c');  
			  }			  			  
          }
        }
		else
		{
          alert("Geocode was not successful for the following reason: " + status);
        }
     });
}

function makecalculateenabled()
{
	$("a[name='calcj']").removeClass('ui-disabled');
}

function makebuttonsdisabled()
{
	$("a[name='calcj']").addClass('ui-disabled');
	$("a[name='endj']").addClass('ui-disabled');
}